import { ArticleModel } from '~/server/models/article.model';
import { CategoryModel } from '~/server/models/category.model';

// 删除分类
export default defineEventHandler(async (event) => {
	const query = getQuery(event);

	const { id } = query;

	if (!id) {
		return { code: 0, message: '缺少分类id' };
	}

	// 支持批量删除，id 可为逗号分隔的多个id
	const ids = (typeof id === 'string' ? id : String(id))
		.split(',')
		.map((item: string) => item.trim())
		.filter(Boolean);

	try {
		await ArticleModel.deleteMany({ categoryId: { $in: ids } });

		await CategoryModel.deleteMany({ _id: { $in: ids } });

		return { code: 1, message: '删除成功' };
	} catch (error: any) {
		return { code: 0, message: error.message };
	}
});
